function readPLIfromFiles(inDir,outDir)

bandlabels={'delta','theta','alpha','beta','gamma'};
ratio=6/6;

for i=1:length(bandlabels)
    
    filter=strcat('pli_',bandlabels{i});
    filter=strcat(filter,'_sp*')
    subjs=dir(fullfile(inDir,filter));
    nsubjs=length(subjs);
    
     
     load(fullfile(inDir,subjs(1).name));
     [nTrial,nch,nch2]=size(pliXT);
    
     
    cond_one=zeros(nsubjs,nch,nch);
    cond_oneS=zeros(nsubjs,nch,nch);    
    cond_two=zeros(nsubjs,nch,nch);
    cond_twoS=zeros(nsubjs,nch,nch);
   
    for j=1:length(subjs)
        
        subjName=subjs(j).name;
        load(fullfile(inDir,subjName));
        
        cond_one(j,:,:)=squeeze(mean(pliXT,1));
        cond_oneS(j,:,:)=findCommonSPLI(spliXT,ratio);
        
        [r,subjName]=strtok(subjName,'-');
        
        subjName=strcat(strcat(strcat('pli_',bandlabels{i}),'_comp'),subjName);
        load(fullfile(inDir,subjName));
        
        cond_two(j,:,:)=squeeze(mean(pliXT,1));
        cond_twoS(j,:,:)=findCommonSPLI(spliXT,ratio);

      
    end
    
    save(fullfile(outDir,strcat('pliXsubjs_',bandlabels{i})),'cond_one','cond_two','cond_oneS','cond_twoS');
end



function spli=findCommonSPLI(spliXT,ratio)

%ratio=3/4;
th=round(size(spliXT,1)*ratio);


indx=spliXT>0;
m=squeeze(sum(indx,1));

tokeep=(m>=th);
spli=sum(spliXT,1);
spli(tokeep)=spli(tokeep)./m(tokeep);
spli(~tokeep)=0;
spli=squeeze(spli);

